/* eslint-disable @typescript-eslint/no-explicit-any */
import { sys } from 'cc';
import { LocalStorage } from '../../Basic/Config/BaseEnum';
import { GameGuideParams, GlobalParams, PlayerParams, ResParams } from '../../Basic/Config/DataConfig';
import { Data } from '../../Basic/Manager/DataManager';
import { MathUtil } from '../../Util/MathUtil';
import { GameManager } from '../../Basic/Manager/GameManager';
import { LevelData } from './GlobalModel';

export class PlayerData {
  // /** 临时数据 */
  // tempSaveData: Map<DataTempParams, any> = new Map();
  /** 玩家属性数据 */
  playerParams: Map<PlayerParams, any> = new Map<PlayerParams, any>();
  globalParams: Map<GlobalParams, any> = new Map();
  guideParams: Map<GameGuideParams, any> = new Map();
  resParams: Map<ResParams, any> = new Map();
  levelData: LevelData;

  constructor() {
    this.playerParams.set(PlayerParams.nickname, '游客');
    this.playerParams.set(PlayerParams.arenaRank, []);
    this.playerParams.set(PlayerParams.playerRank, []);
    this.playerParams.set(PlayerParams.avatarUrl, '');
    this.playerParams.set(PlayerParams.level, 1);
    this.playerParams.set(PlayerParams.trophy, 0);
    this.playerParams.set(PlayerParams.isNovice, true);
    this.playerParams.set(PlayerParams.isFirstPlay, true);
    this.playerParams.set(PlayerParams.getUserInfo, false);
    // this.playerParams.set(PlayerParams.gameMode, GameMode.NORMAL);
    this.playerParams.set(PlayerParams.gameLevel, 1);
    this.playerParams.set(PlayerParams.lastLiveDay, -1);
    this.playerParams.set(PlayerParams.adCount, 0);
    this.playerParams.set(PlayerParams.activeDays, 0);
    this.playerParams.set(PlayerParams.maxStage, 0);
    this.playerParams.set(PlayerParams.openId, MathUtil.UUID());

    this.globalParams.set(GlobalParams.gameMusic, true);
    this.globalParams.set(GlobalParams.gameSound, true);
    this.globalParams.set(GlobalParams.gameShark, true);
    this.globalParams.set(GlobalParams.gameWinCombe, 0);
    this.globalParams.set(GlobalParams.gameLevel, 1);
    this.globalParams.set(GlobalParams.gameing, false);
    this.globalParams.set(GlobalParams.gameChallenge, new Map());

    this.resParams.set(ResParams.resCoin, 0);
    this.resParams.set(ResParams.resDiamond, 0);
    this.resParams.set(ResParams.resPropKey, 3);
    this.resParams.set(ResParams.resPropReset, 0);
    this.resParams.set(ResParams.resPropBrush, 0);
    this.resParams.set(ResParams.resPropBottle, 0);
    this.resParams.set(ResParams.resPropSort, 0);

    this.guideParams.set(GameGuideParams.move, 100);
    this.guideParams.set(GameGuideParams.prop, 200);
    this.guideParams.set(GameGuideParams.novice, 0);

    this.levelData = {
      level: -1,
      wake: -1,
      time: -1,
      progress: -1,
      bolletData: [],
      seatData: {
        seatArr: [],
        slotArr: [],
      },
      blockData: [],
    };
  }

  Save(keys: string = '') {
    if (keys != '') {
      switch (keys) {
        case 'player':
          Data.saveData(LocalStorage.PlayerParams, this.playerParams);
          break;
        case 'global':
          Data.saveData(LocalStorage.GlobalParams, this.globalParams);
          break;
        case 'res':
          Data.saveData(LocalStorage.ResParam, this.resParams);
          break;
        case 'guide':
          Data.saveData(LocalStorage.GameGuide, this.guideParams);
          break;
        case 'level':
          Data.saveData(LocalStorage.LevelData, this.levelData);
          break;
      }
    } else {
      Data.saveData(LocalStorage.PlayerParams, this.playerParams);
      Data.saveData(LocalStorage.GlobalParams, this.globalParams);
      Data.saveData(LocalStorage.ResParam, this.resParams);
      Data.saveData(LocalStorage.GameGuide, this.guideParams);
      Data.saveData(LocalStorage.LevelData, this.levelData);
    }
  }

  initFirst() {
    const data = sys.localStorage;
    // console.log('version ', version);
    const playerData = data.getItem(LocalStorage.PlayerParams);
    if (playerData) {
      const param = JSON.parse(playerData);
      for (const key in param) {
        this.initParams(key, param[key], LocalStorage.PlayerParams);
      }
    }

    const globalData = data.getItem(LocalStorage.GlobalParams);
    if (globalData) {
      const param = JSON.parse(globalData);
      for (const key in param) {
        this.initParams(key, param[key], LocalStorage.GlobalParams);
      }
    }

    const resData = data.getItem(LocalStorage.ResParam);
    if (resData) {
      const param = JSON.parse(resData);
      for (const key in param) {
        this.initParams(key, param[key], LocalStorage.ResParam);
      }
    }

    const guideData = data.getItem(LocalStorage.GameGuide);
    if (guideData) {
      const param = JSON.parse(guideData);
      for (const key in param) {
        this.initParams(key, param[key], LocalStorage.GameGuide);
      }
    }

    const levelData = data.getItem(LocalStorage.LevelData);
    if (levelData) {
      const param = JSON.parse(levelData);
      for (const key in param) {
        this.initParams(key, param[key], LocalStorage.LevelData);
      }
    }

    this.Save();

    // const nowTime = Date.now();
    // if (this.globalParams.get(GlobalParams.gameOfflineTime) > 0) {
    //   const spaceTime = Math.floor((Date.now() - this.globalParams.get(GlobalParams.gameOfflineTime)) / 1000);
    //   // console.log(`离线时间`, DateUtil.getTimeStr(`{dd}天{hh}时{mm}分{ss}秒`, spaceTime * 1000));
    //   this.globalParams.get(GlobalParams.gameTimeAward).forEach((item) => {
    //     if (item.time > 0) {
    //       item.time -= spaceTime;
    //       item.time = item.time <= 0 ? 0 : item.time;
    //     }
    //   });
    //   MenuModel_Ins.offlineTime = spaceTime;
    // }
    // const curDay = new Date().setHours(0, 0, 0, 0);
    // const lastDay = new Date(Number(this.playerParams.get(PlayerParams.lastLiveDay))).setHours(0, 0, 0, 0);
    // const oneDay = 24 * 60 * 60 * 1000;
    // if (this.playerParams.get(PlayerParams.lastLiveDay) === -1 || curDay - lastDay >= oneDay) {
    //   this.playerParams.set(PlayerParams.lastLiveDay, curDay);
    //   this.changePlayerAttr(PlayerParams.activeDays, 1);
    //   this.changeGlobalAttr(GlobalParams.gameEnergyEveryDay, true);
    //   this.gameRes.get(ResParams.resShopChest).adCount = 0;

    //   // this.dialogConfigParams.set(DialogParams.Gift_1, false);
    //   // this.dialogConfigParams.set(DialogParams.Gift_2, false);
    //   this.dialogConfigParams.set(DialogParams.Gift_3, 1);
    //   this.dialogConfigParams.set(DialogParams.Gift_4, 1);
    //   this.dialogConfigParams.set(DialogParams.Gift_5, 1);
    //   this.feedState.get(1).state = true;
    //   this.feedState.get(2).state = true;
    //   this.feedState.get(3).state = true;
    //   // 判断上次登录的时间
    //   // 刷新每日礼包状态
    //   const shopStateData: Map<number, any> = this.globalParams.get(GlobalParams.gameShopItemStatus) as Map<number, any>;
    //   if (shopStateData.has(1001)) {
    //     shopStateData.get(1001).state = false;
    //     shopStateData.get(1001).buyCount = 0;
    //   }
    //   if (shopStateData.has(1002)) {
    //     shopStateData.get(1002).state = false;
    //     shopStateData.get(1002).buyCount = 0;
    //   }

    //   const dayAward: number[] = [];
    //   const weekAward: number[] = [];
    //   const monthAward: number[] = [];

    //   shopStateData.forEach((value, key) => {
    //     const index = Math.floor(key / 100);
    //     switch (index) {
    //       case 20:
    //         dayAward.push(key);
    //         break;
    //       case 21:
    //         weekAward.push(key);
    //         break;
    //       default:
    //         monthAward.push(key);
    //         break;
    //     }
    //   });
    //   // 刷新每日礼包状态
    //   dayAward.forEach((key) => {
    //     shopStateData.get(key).state = false;
    //     shopStateData.get(key).buyCount = 0;
    //   });

    //   // 刷新每周礼包状态
    //   const date: Date = new Date();
    //   date.setDate(date.getDate() - date.getDay() + (date.getDay() === 0 ? -6 : 1));
    //   date.setHours(0, 0, 0, 0);
    //   const curWeek = date.getTime();
    //   weekAward.forEach((key) => {
    //     if (shopStateData.get(key).time < curWeek) {
    //       shopStateData.get(key).state = false;
    //       shopStateData.get(key).buyCount = 0;
    //     }
    //   });
    //   // 刷新每月礼包状态
    //   date.setDate(1);
    //   date.setHours(0, 0, 0, 0);
    //   const curMonth = date.getTime();
    //   monthAward.forEach((key) => {
    //     if (shopStateData.get(key).time < curMonth) {
    //       shopStateData.get(key).state = false;
    //       shopStateData.get(key).buyCount = 0;
    //     }
    //   });

    //   if (this.gameRes.get(ResParams.resArenaTicket) < 5) this.changeResAttr(ResParams.resArenaTicket, 5, true);
    //   if (this.gameRes.get(ResParams.resTowerVoucher) < 3) this.changeResAttr(ResParams.resTowerVoucher, 3, true);
    // }
    // this.globalParams.set(GlobalParams.gameOfflineTime, nowTime);
    // Data.saveData(LocalStorage.GlobalParams, this.globalParams);
    // Data.saveData(LocalStorage.ResParam, this.gameRes);
    // Data.saveData(LocalStorage.DialogConfigParams, this.dialogConfigParams);
    // Data.saveData(LocalStorage.FeedState, this.feedState);

    //记录登录时间
    this.playerParams.set(PlayerParams.onlineTime, GameManager.TimeControl.nowTime);
  }
  init() {
    const data = sys.localStorage;
  }
  /** 初始化参数 */
  initParams(key, param, index) {
    switch (index) {
      case LocalStorage.PlayerParams:
        this.playerParams.set(key as PlayerParams, param);
        break;
      case LocalStorage.GlobalParams:
        if ((key as GlobalParams) == GlobalParams.gameChallenge) {
          const state = new Map<number, any>();
          try {
            for (const key of Object.keys(param)) {
              state.set(Number(key), param[key]);
            }
          } catch {
            // console.log(`${key}'s data is null`);
          } finally {
            this.globalParams.set(key as GlobalParams, state);
          }
        } else {
          this.globalParams.set(key as GlobalParams, param);
        }

        break;
      case LocalStorage.ResParam:
        this.resParams.set(Number(key), param);
        break;
      case LocalStorage.GameGuide:
        this.guideParams.set(key as GameGuideParams, param);
        break;
      case LocalStorage.LevelData:
        this.levelData[key] = param;
        break;
    }
  }

  changePlayerAttr(attrId: PlayerParams, value: number | string | boolean | any, reset: boolean = false) {
    let temp;
    if (typeof value == 'number') {
      if (reset || !this.playerParams.has(attrId)) {
        this.playerParams.set(attrId, value);
      } else {
        temp = (this.playerParams.get(attrId) as number) + value;
        this.playerParams.set(attrId, temp);
      }
    } else {
      temp = value;
      this.playerParams.set(attrId, value);
    }
    Data.saveData(LocalStorage.PlayerParams, this.playerParams);
  }
  changeGlobalAttr(attrId: GlobalParams, value: number | string | boolean | any, reset: boolean = false) {
    let temp;
    if (typeof value == 'number') {
      if (reset || !this.globalParams.has(attrId)) {
        this.globalParams.set(attrId, value);
      } else {
        temp = (this.globalParams.get(attrId) as number) + value;
        this.globalParams.set(attrId, temp);
      }
    } else {
      temp = value;
      this.globalParams.set(attrId, value);
    }
    Data.saveData(LocalStorage.GlobalParams, this.globalParams);
  }
  changeResAttr(attrId: ResParams, value: number | string | boolean | any, reset: boolean = false) {
    let temp;
    if (typeof value == 'number') {
      if (reset || !this.resParams.has(attrId)) {
        this.resParams.set(attrId, value);
      } else {
        temp = (this.resParams.get(attrId) as number) + value;
        this.resParams.set(attrId, temp);
      }
    } else {
      temp = value;
      this.resParams.set(attrId, value);
    }
    Data.saveData(LocalStorage.ResParam, this.resParams);
  }
  changeGuideAttr(attrId: GameGuideParams, value: number | string | boolean | any, reset: boolean = false) {
    let temp;
    if (typeof value == 'number') {
      if (reset || !this.guideParams.has(attrId)) {
        this.guideParams.set(attrId, value);
      } else {
        temp = (this.guideParams.get(attrId) as number) + value;
        this.guideParams.set(attrId, temp);
      }
    } else {
      temp = value;
      this.guideParams.set(attrId, value);
    }
    Data.saveData(LocalStorage.GameGuide, this.guideParams);
  }
}
